package com.eleaf.adbb.model;

import java.io.File;

import com.eleaf.adbb.util.BaseUtil;

public class DefaultNameGenerater implements NameGenerater {

	private static final String LAYOUT_FILE_TYPE = "xml";
	private static final String ACTIVITY = "activity";
	private static final String FRAGMENT = "fragment";

	@Override
	public String generateVariableName( String id ) {
		String[] split = id.split ( "_" );
		for ( int i = 0; i < split.length; i++ ) {
			split[i] = split[i].trim ();
			if ( split[i].length () > 0 )
				split[i] = BaseUtil.appendString ( split[i].substring ( 0, 1 ).toUpperCase (), split[i].substring ( 1, split[i].length () ) ).trim ();
		}
		return BaseUtil.appendString ( "m", BaseUtil.appendString ( split ) );
	}

	@Override
	public String generateClassName( String layoutName, LayoutType type ) {
		String[] split = layoutName.split ( "_" );
		for ( int i = 0; i < split.length; i++ ) {
			split[i] = split[i].trim ();
			if ( split[i].length () > 0 )
				split[i] = BaseUtil.appendString ( split[i].substring ( 0, 1 ).toUpperCase (), split[i].substring ( 1, split[i].length () ) ).trim ();
		}
		return BaseUtil.appendString ( BaseUtil.appendString ( split ) );
	}

	@Override
	public String generateHostPackage() {
		return "com.jeeinc.together.app";
	}

	@Override
	public String generateLastPackage( LayoutType type ) {
		if ( type == LayoutType.ACTIVITY )
			return "ui";
		else
			return "ui.fragment";
	}

	@Override
	public boolean layoutFileFilte( File f ) {
		String fileName = f.getName ();
		if ( fileName == null || fileName.trim ().isEmpty () )
			return false;
		String prefix = fileName.substring ( fileName.lastIndexOf ( "." ) + 1 );
		if ( prefix == null || prefix.trim ().isEmpty () )
			return false;
		if ( prefix.toLowerCase ().equals ( LAYOUT_FILE_TYPE ) ) {
			fileName = fileName.substring ( 0, fileName.lastIndexOf ( "." ) );
			String[] split = fileName.split ( "_" );
			if ( split.length < 2 )
				return false;
			String type = split[split.length - 1].toLowerCase ();
			if ( type.equals ( FRAGMENT ) || type.equals ( ACTIVITY ) ) {
				return true;
			}
		}
		return false;
	}
}
